package com.arvin.skin.core.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Build;

import com.arvin.skin.core.SkinAttribute;

import java.lang.reflect.AnnotatedElement;

import skin.arvin.com.skin_core.R;

public class SkinThemeUtils {
    private static int[] APPCOMPAT_COLOR_PRIMARY_DARK_ATTRS={
      android.support.v7.appcompat.R.attr.colorPrimaryDark
    };
    private static int[] STATUSBAR_COLOR_ATTRS ={
            android.R.attr.statusBarColor, android.R.attr.navigationBarColor
    };

    private static int[] TYPEFACE_ATTR ={
            R.attr.skinTypeface
    };
    public static int[] getResId(Context context,int[] attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int[] resIds = new int[attrs.length];
        for (int i = 0; i < attrs.length; i++) {
            resIds[i] = typedArray.getResourceId(i,0);
        }
        typedArray.recycle();
        return resIds;
    }

    public static void updateStatusBar(Activity activity) {
        int[] resIds = getResId(activity,STATUSBAR_COLOR_ATTRS);
        if (resIds[0]==0) {
            int statusBarColorId = getResId(activity, APPCOMPAT_COLOR_PRIMARY_DARK_ATTRS)[0];
            if (statusBarColorId==0){
                return;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.getWindow().setStatusBarColor(SkinResources.getInstance().getColor(statusBarColorId));
            }

        }else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.getWindow().setStatusBarColor(SkinResources.getInstance().getColor(resIds[0]));
            }
        }
        if (resIds[1]!=0){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.getWindow().setNavigationBarColor(SkinResources.getInstance().getColor(resIds[1]));
            }
        }

    }

    public static Typeface getSkinTypeface(Activity activity) {
        int skinTypefaceId = getResId(activity,TYPEFACE_ATTR)[0];
        Typeface typeface = SkinResources.getInstance().getTypeface(skinTypefaceId);
        return typeface;
    }
}
